package cn.ticket.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Authuser;
import cn.ticket.service.AuthUserService;


@Controller
public class MainController {
	@Autowired
	AuthUserService authUserService;
	@RequestMapping("/")
	public String index(ModelMap modelMap,HttpSession session){
		String username=(String) session.getAttribute("username");
		if (username==null) {
			return "login";
		}
		return "holi_bus";
	}
	@RequestMapping("/holi_bus")
	public String holiBus(){
		return "holi_bus";
	}
	@RequestMapping("/holi_bus_detail")
	public String holiBusDetail(){
		return "holi_bus_detail";
	}
	@RequestMapping("/holi_ticket")
	public String holiTicket(){
		return "holi_ticket";
	}
	@RequestMapping("/holi_ticketed")
	public String holiTicketed(){
		return "holi_ticketed";
	}
	@RequestMapping("/work_ticket")
	public String workTicket(){
		return "work_ticket";
	}
	@RequestMapping("/work_ticketed")
	public String workTicketed(){
		return "work_ticketed";
	}
	@RequestMapping("/work_bus")
	public String workBus(){
		return "work_bus";
	}
	@RequestMapping("/work_bus_detail")
	public String workBusDetail(){
		return "work_bus_detail";
	}
	@RequestMapping("/login")
	public String login(){
		return "login";
	}

	@RequestMapping("/busDetail")
	public String busDetail(){
		return "busDetail";
	}
	@RequestMapping("/carPooling")
	public String car(){
		return "carPooling";
	}
	@RequestMapping(value = "/authority", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public String authority(ModelMap modelMap, @RequestParam("username") String username,@RequestParam("password") String password,HttpSession session,HttpServletRequest request){
		if(authUserService.checkUserAccess(username, password)){
			session.setAttribute("username", username);			
			//String url=request.getServletPath();
	//		AuthUser user = authService.getUserByUsername(username);			
		
			
			return "redirect:/";
		}else{
			return "redirect:login";
		}	
	}
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		HttpSession session  = request.getSession();
		Object username = session.getAttribute("username");
		if (username!=null) {
			session.removeAttribute("username");
		}
		return "login";
	}
}
